<template>
  <div>
    <div class="head">Scientific Inquiry</div>
    <div class="img-wrapper">
      <div class="img-box">
      </div>
    </div>
    <van-form class="login-form">
      <van-field v-model="data.user.userTel"
                 placeholder="输入手机号"
                 left-icon="user-circle-o"
                 :rules="[{ required: true, message: '请输入合法的手机号' }]" />
      <van-field v-model="data.user.password"
                 type="password"
                 placeholder="输入密码"
                 left-icon="lock"
                 :rules="[{ required: true, message: '请输入密码！' }]" />
      <van-radio-group class="login-radio"
                       v-model="data.user.userGroup"
                       icon-size="15"
                       checked-color="#fdb52b"
                       direction="horizontal">
        <van-radio name="1">学生端</van-radio>
        <van-radio name="2">教师端</van-radio>
        <van-radio name="3">管理员端</van-radio>
      </van-radio-group>
      <div class="login-form-submit">
        <van-button round
                    block
                    class="login-form-submit-btn"
                    color="#87d4eb"
                    native-type="submit"
                    text="登录"
                    @click="handleSubmit">
        </van-button>
        <van-button round
                    block
                    class="login-form-submit-btn"
                    color="#6ec8c1"
                    native-type="submit"
                    text="游客模式"
                    @click="handleVisitor()">
        </van-button>
        <router-link to=""
                     class="login-form-forget">忘记密码？</router-link>
      </div>
    </van-form>
  </div>
</template>
<script>
import { Toast } from 'vant'
import { reactive, getCurrentInstance } from 'vue'
import { localSet, localGet } from '../utils/auth.js'
export default {
  setup () {
    const { proxy } = getCurrentInstance()
    const data = reactive({
      user: {
        userTel: '',
        password: '',
        userGroup: '1'
      }
    })
    function handleSubmit () {
      proxy.$axios.user.login(data.user).then(res => {
        if (res.status === 200) {
          let destiantion = ''
          if (data.user.userGroup === '1') {
            destiantion = '/user'
          } else if (data.user.userGroup === '2') {
            destiantion = '/teacher/home'// 教师端入口
          } else {
            destiantion = '/manage/home'// 管理员端入口
          }
          localSet('STEM_TOKEN', res.data.data.token)
          window.sessionStorage.setItem('token', res.data.data.token)
          window.sessionStorage.setItem('userId', res.data.data.user.userId)
          // console.log(localGet('STEM_TOKEN'))
          // console.log(res)
          Toast.success('登录成功')
          // proxy.$store.dispatch('setUser', res.data.data.user)
          localSet('STEM_USER_INFO', res.data.data.user)
          console.log(localGet('STEM_USER_INFO'))
          proxy.$router.push(destiantion)
        } else {
          const msg = res.data.message
          Toast.fail(msg)
        }
      })
    }
    function handleVisitor () {
      proxy.$router.push('/')
    }
    return {
      data,
      handleSubmit,
      handleVisitor
    }
  }
}
</script>
<style lang="scss" scoped>
.head {
  height: 46px;
  line-height: 46px;
  padding-left: $margin-horizon;
  background-color: $theme-color1;
  color: $theme-color3;
  font-size: $fontsize-3;
  font-weight: 700;
  box-shadow: 0px 1px 1px 0px rgba(0, 0, 0, 0.5);
}
.img-box {
  margin: 0 auto;
  width: 270px;
  height: 270px;
  background: url(@/assets/images/mechine.png) no-repeat;
  background-size: 100% 100%;
}
.login-radio {
  margin-top: $margin-verticle;
}
.login-form {
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 80%;
  .van-field:nth-child(2) {
    margin-top: $margin-verticle;
  }
  &-submit {
    width: 150px;
    margin: 20px auto;
    text-align: center;
    font-size: $fontsize-5;
    text-decoration: underline;
    &-btn {
      margin-left: 20px;
      margin-bottom: 20px;
      width: 100px;
      height: 30px;
    }
  }
  &-forget {
    display: inline-block;
  }
}
</style>
